#include "DrawHelper.h"
#include <Cg/cg.h>
#include <Cg/cgGL.h>
#include <cmath>

void DrawHelper::drawStar(float x, float y,int starPoints, float R, float r)
{
	int i;
	float piOverStarPoints = (float)(3.14159 / starPoints),
		angle = 0.0;

	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(x, y);  /* Center of star */
	/* Emit exterior vertices for star's points. */
	for (i = 0; i < starPoints; i++) {
		glVertex2f(x + R * cos(angle), y + R * sin(angle));
		angle += piOverStarPoints;
		glVertex2f(x + r * cos(angle), y + r * sin(angle));
		angle += piOverStarPoints;
	}
	/* End by repeating first exterior vertex of star. */
	angle = 0;
	glVertex2f(x + R * cos(angle), y + R * sin(angle));
	glEnd();
}

/* Apply an inefficient but simple-to-implement subdivision scheme for a triangle. */
void DrawHelper::triangleDivide(int depth,const float a[2], const float b[2], const float c[2],const float ca[3], const float cb[3], const float cc[3])
{
	if (depth == 0) {
		glColor3fv(ca);
		glVertex2fv(a);
		glColor3fv(cb);
		glVertex2fv(b);
		glColor3fv(cc);
		glVertex2fv(c);
	}
	else {
		const float d[2] = { (a[0] + b[0]) / 2, (a[1] + b[1]) / 2 },
			e[2] = { (b[0] + c[0]) / 2, (b[1] + c[1]) / 2 },
			f[2] = { (c[0] + a[0]) / 2, (c[1] + a[1]) / 2 };
		const float cd[3] = { (ca[0] + cb[0]) / 2, (ca[1] + cb[1]) / 2, (ca[2] + cb[2]) / 2 },
			ce[3] = { (cb[0] + cc[0]) / 2, (cb[1] + cc[1]) / 2, (cb[2] + cc[2]) / 2 },
			cf[3] = { (cc[0] + ca[0]) / 2, (cc[1] + ca[1]) / 2, (cc[2] + ca[2]) / 2 };

		depth -= 1;
		triangleDivide(depth, a, d, f, ca, cd, cf);
		triangleDivide(depth, d, b, e, cd, cb, ce);
		triangleDivide(depth, f, e, c, cf, ce, cc);
		triangleDivide(depth, d, e, f, cd, ce, cf);
	}
}